package keeto.com.vn.classified.task;

import java.io.IOException;

import keeto.com.vn.classified.core.ClassifiedCore;
import keeto.com.vn.classified.core.ClassifiedListener;
import keeto.com.vn.classified.network.IProtocol;
import keeto.com.vn.classified.request.GetProductAdRequest;
import keeto.com.vn.classified.response.GetProductAdResponse;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;


public class TaskGetProductAd extends AbstractTask {
	
	int pageLimit;
	String sortBy;
	int pageNumber;
	String Area;
	
	public TaskGetProductAd(ClassifiedListener listener, ClassifiedCore core,int pageLimit, String sortBy, int pageNumber,String Area) {
		super(listener, core);
		this.pageLimit = pageLimit;
		this.sortBy = sortBy;
		this.pageNumber = pageNumber;
		this.Area = Area;
		}

	@Override
	public void run() throws ClientProtocolException, JSONException,
			IOException {
		GetProductAdRequest request = new GetProductAdRequest();
		request.setPageLimit(pageLimit);
		request.setSortBy(sortBy);
		request.setPageNumber(pageNumber);
		request.setArea(Area);
		GetProductAdResponse reponse = json.getAndroidProductAd(request);
		for(ClassifiedListener listener : listeners){
			listener.finishGetProductAd(reponse.getReturnCode(), reponse.getList_latest(), reponse.getList_interest(), reponse.getList_appreciate(), reponse.getPageCount());
		}
	}
	@Override
	public void timeoutCallback() {
		for(ClassifiedListener ilistener : core.copyAndReconstructListeners(null)){
			ilistener.finishGetProductAd(IProtocol.STATUS_CODE_CONNECTION_TIMEOUT_502, null, null, null, 0);
		}
	}
	
	
}
